(function() {
    angular.module('app').factory('dashPieService', dashPieService);

    dashPieService.$inject = ['util', '$q'];

    function dashPieService(util, $q) {
        return {
            getHttp: get
        }
        /**
         * [get description]
         * @param  {[type]}  catName    [description]
         * @param  {[type]}  pieOption  [饼图optiuon]
         * @param  {[type]}  lineOption [柱状图option]
         * @param  {Boolean} isLeaf     [是否有叶子]
         * @return {[type]}             [description]
         */
        function get(catName, pieOption, lineOption, isLeaf) {
            pieOption.series[0].data.length = 0;
            lineOption.xAxis.data.length = 0;
            lineOption.series[0].data.length = 0;


            var defer = $q.defer();
            // console.log('http-isLeaf:'+isLeaf);
            var url = isLeaf == true ? '/marketconditions/statistic/getCategoryStatistic.htm' : '/marketconditions/statistic/getCategoryStatistics.htm';
            util.$get({
                url: url,
                data: {
                    catName: catName
                }
            }).then(function(resp) {
                // console.log('resp');
                // console.log(resp);
                if (isLeaf) { //叶子 天猫，淘宝，天猫国际，全球购
                    if (resp.value.length > 0) { //it has data
                        var leafData = resp.value[0];
                        pieOption.series[0].data = [{
                            name: '天猫',
                            value: leafData.tSellMoney,
                            click:false
                        }, {
                            name: '淘宝',
                            value: leafData.cSellMoney,
                            click:false
                        }, {
                            name: '天猫国际',
                            value: leafData.thkSellMoney,
                            cliak:false
                        }, {
                            name: '全球购',
                            value: leafData.gSellMoney,
                            click:false
                        }];


                        lineOption.xAxis.data = ['天猫', '淘宝', '天猫国际', '全球购'];
                        lineOption.series[0].data = [leafData.tSellMoney, leafData.cSellMoney, leafData.thkSellMoney, leafData.gSellMoney];
                    } else {

                    }

                } else {
                    angular.forEach(resp.value, function(item, index) {

                        var categoryName = item.categoryName.indexOf('--') == -1 ? item.categoryName : item.categoryName.split('--')[item.categoryName.split('--').length - 1];

                        if (index > 24) {
                            pieOption.series[0].data.push({
                                name: categoryName,
                                value: item.sellMoney,
                                isLeaf: item.isLeaf,
                                label: {
                                    normal: {
                                        show: false,
                                        textStyle: {
                                            fontSize: 12
                                        }
                                    },
                                    emphasis: {
                                        show: true,
                                        textStyle: {
                                            fontSize: 14,
                                            fontWeight: 800
                                        }
                                    }
                                },
                                labelLine: {
                                    normal: {
                                        show: false,
                                        smooth: true,
                                        lineStyle: {
                                            opacity: 0.7
                                        },
                                    },
                                    emphasis: {
                                        show: true,
                                        lineStyle: {
                                            width: 3,
                                            opacity: 1
                                        },
                                    }
                                }
                            });
                        } else {
                            pieOption.series[0].data.push({
                                name: categoryName,
                                value: item.sellMoney,
                                isLeaf: item.isLeaf
                            });
                        }

                        lineOption.xAxis.data.push(categoryName);
                        lineOption.series[0].data.push(item.sellMoney);

                    });
                }

                defer.resolve();
            }, function(err) {
                defer.reject();
            });

            return defer.promise;
        }

        function leafSetOption() {

        }
    }
})();
